﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Dynamic;
using WebApp.Data;
using WebApp.Commons;
using System.Transactions;

namespace WebApp.Services
{
    public class TheProjectService
    {

        public string[] CreateOrUpdateProject(tbl_project project) 
        {
            string[] output = new string[3];
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    tbl_project eModel;
                    if (project.project_id == 0)
                    {
                        project.project_create_by = PavingContext.CurrentUserId();
                        project.project_create_date = DateTime.Now;

                        db.tbl_projects.InsertOnSubmit(project);
                        db.SubmitChanges();
                        var count = 0;
                        var data = this.GetProjectListByCustId(0, 0, "", "desc", project.customer_id.ToString(), ref count);
                        output[0] = data[0].project_id.ToString();
                    }
                    else
                    {
                        eModel = db.tbl_projects.SingleOrDefault(s => s.project_id.Equals(project.project_id));
                        output[0] = project.project_id.ToString();
                        if (eModel != null)
                        {
                            if(!string.IsNullOrEmpty(project.project_location))
                                eModel.project_location = project.project_location;
                            if (project.proj_worker!=0)
                                eModel.proj_worker = project.proj_worker;

                            eModel.project_update_by = PavingContext.CurrentUserId(); ;
                            eModel.project_update_date = DateTime.Now;
                        }
                        db.SubmitChanges();
                    }

                    output[1] = "true";
                    return output;
                }
            }
            catch (Exception ex)
            {
                output[1] = "false";
                output[2] = ex.Message;
                return output;
            }
        }

        public bool Delete(int[] ids)
        {
            using (var tran = new TransactionScope())
            {
                try
                {
                    using (var db = new PAVINGDataContext())
                    {

                        foreach (var id in ids)
                        {
                            var eModel = db.tbl_projects.SingleOrDefault(s => s.project_id.Equals(id));

                            if (eModel == null) continue;
                            db.tbl_projects.DeleteOnSubmit(eModel);
                            db.SubmitChanges();
                        }

                        tran.Complete();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public List<tbl_project> GetAreaType(int index,
                                       int size,
                                       string sortColumn,
                                       string sortDirection,
                                       string keyword, string keyword2, string keyword3, string keyword4,
                                       ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                //count = db.tbl_customers.Count(u => u.customer_firstname.Equals(keyword));

                var projs = db.tbl_projects.Any(p => ((string.IsNullOrEmpty(keyword)) || (p.tbl_customer.customer_firstname.Contains(keyword)))
                                                     & ((string.IsNullOrEmpty(keyword2)) || (p.tbl_customer.customer_tel.Contains(keyword2)))
                                                     & ((string.IsNullOrEmpty(keyword3)) || (p.project_location.Contains(keyword3)))
                                                     & ((string.IsNullOrEmpty(keyword4)) || (p.project_create_date.Equals(keyword4))));

                //return db.tbl_projects.Where(u => u.tbl_customer.customer_firstname.Contains(keyword)).ToList();
                return db.tbl_projects.Where(u => ((string.IsNullOrEmpty(keyword)) || (u.tbl_customer.customer_firstname.Contains(keyword)))
                                                     & ((string.IsNullOrEmpty(keyword2)) || (u.tbl_customer.customer_tel.Contains(keyword2)))
                                                     & ((string.IsNullOrEmpty(keyword3)) || (u.project_location.Contains(keyword3)))
                                                     & ((string.IsNullOrEmpty(keyword4)) || (u.project_create_date.Equals(keyword4)))).ToList();
            }

        }

        public List<tbl_project> GetProjectListByCustId(int index,
                                      int size,
                                      string sortColumn,
                                      string sortDirection,
                                      string custId,
                                      ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                //count = db.tbl_customers.Count(u => u.customer_firstname.Equals(keyword));

                //return db.tbl_projects.Where(p => ((!string.IsNullOrEmpty(custId)) && (p.customer_id.ToString().Equals(custId)))).OrderBy(direction).ToList();
                return db.tbl_projects.Where(p => ((!string.IsNullOrEmpty(custId)) && (p.customer_id.ToString().Equals(custId)))).OrderByDescending(x => x.project_id).ToList();
            }
        }

        public tbl_customer GetCustName(int index)
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_customers.SingleOrDefault(u => u.customer_id.Equals(index));
            }

        }

        public List<tbl_project_area> GetProjectArea(int index,
                                      int size,
                                      string sortColumn,
                                      string sortDirection, string project_id, ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_project_areas.Where(p => ((!string.IsNullOrEmpty(project_id)) && (p.project_id.ToString().Equals(project_id)))).ToList();
            } 
        }

        public List<tbl_area_type> GetAreaType() 
        {
            using(var db = new PAVINGDataContext())
            {
                return db.tbl_area_types.ToList();
            }
        }

        public bool UpdateProjectArea(tbl_project_area pArea) 
        {
            try
            {
                using (var db = new PAVINGDataContext()) 
                {
                    if (pArea.project_area_id == 0)
                    {
                        pArea.project_area_create_by = PavingContext.CurrentUserId();
                        pArea.project_area_create_date = DateTime.Now;

                        string newProjAreaDigit = (DataProvider.GetLastProjectAreaId() + 1).ToString();
                        int newId;
                        pArea.project_area_id = int.TryParse(newProjAreaDigit, out newId) ? newId : 0;

                        db.tbl_project_areas.InsertOnSubmit(pArea);
                    }
                    else
                    {
                        tbl_project_area projArea = db.tbl_project_areas.SingleOrDefault(p => p.project_area_id.Equals(pArea.project_area_id));
                        if (projArea != null)
                        {
                            if (pArea.area_type_id != 0)
                                projArea.area_type_id = pArea.area_type_id;
                            if(projArea.is_active !=null)
                                projArea.is_active = pArea.is_active;
                            if (!string.IsNullOrEmpty(pArea.project_area_image))
                                projArea.project_area_image = pArea.project_area_image;
                            if (!string.IsNullOrEmpty(pArea.project_area_outline_url))
                                projArea.project_area_outline_url = pArea.project_area_outline_url;
                            if (pArea.project_area_size != 0)
                                projArea.project_area_size = pArea.project_area_size;
                            if (!string.IsNullOrEmpty(pArea.project_paving_type))
                                projArea.project_paving_type = pArea.project_paving_type;

                            projArea.project_area_update_by = PavingContext.CurrentUserId();
                            projArea.project_area_update_date = DateTime.Now;
                        }
                    }
                    db.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("UpdateProjectArea : " + ex.Message);
                return false;
            }
        }

        public bool DeleteProjectArea(tbl_project_area model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    var selectedModel = db.tbl_project_areas.SingleOrDefault(s => s.project_area_id.Equals(model.project_area_id));

                    if (selectedModel != null)
                    {
                        db.tbl_project_areas.DeleteOnSubmit(selectedModel);
                        db.SubmitChanges();

                        return true;
                    }

                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DeleteProjectArea : " + ex.Message);
                return false;
            }
        }

        //public tbl_area_type_step GetAreaTypeStep()
        //{
        //    using(var db = new PAVINGDataContext())
        //    {
        //        return db.tbl_area_type_steps.Where(s => s.
        //    }
        //}
    }
}